﻿Imports DAO
Imports DTO

Public Class PhieuThuBUS

    Public Sub ThemPhieuThu(ByVal phieu As PhieuThuDTO)
        Dim ngon As Integer = NgonNgu.ngon
        
        Dim kh As New KhachHangDAO ' ' ' 'trong bang phiêu thu có id khách hàng
        Dim dt As New DataTable

        dt = kh.LayTienNo(phieu)
        Dim tienno As Integer = Integer.Parse(dt(0)("TongTienNo"))
        If tienno = 0 Then
            If ngon = 0 Then
                Throw New Exception("Khách hàng không có nợ tiền.")
            Else
                Throw New Exception(" Customers do not have debt")
            End If
        End If
        If tienno < phieu.LayTienThu Then
            If ngon = 0 Then
                Throw New Exception("Số tiền thu lớn hơn số tiền khách hàng đang nợ")
            Else
                Throw New Exception("Proceeds larger customers who owe money")
            End If
        End If
        Dim p As New PhieuThuDAO
        p.ThemPhieuThu(phieu)
    End Sub

    Public Function LayPhieuThu(ByVal pt As PhieuThuDTO)
        Dim dt As New DataTable
        Dim phieu As New PhieuThuDAO
        dt = phieu.LayPhieuThu(pt)
        Return dt
    End Function

    Public Sub XoaPhieuThu(ByVal pt As PhieuThuDTO)
        Dim phieu As New PhieuThuDAO
        phieu.XoaPhieuThu(pt)
    End Sub

    Public Function RefreshPhieuThu(ByVal pt As PhieuThuDTO) As DataTable
        Dim dt As New DataTable
        Dim phieu As New PhieuThuDAO
        dt = phieu.RefreshPhieuThu(pt)
        Return dt
    End Function

    Public Function LayTienNo(ByVal p As PhieuThuDTO) As Integer
        Dim no As Integer
        Dim dt As New DataTable
        Dim phieu As New KhachHangDAO
        dt = phieu.LayTienNo(p)
        no = Integer.Parse(dt.Rows(0).ItemArray(0).ToString())
        Return no
    End Function

    Public Function LoadPhieuThu()
        Dim dt As New DataTable
        Dim p As New PhieuThuDAO
        dt = p.loadPhieuThu()
        Return dt
    End Function

End Class
